package com.ssm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils; //导入方法依赖的package包/类

import com.ssm.mapper.UserMapper;
import com.ssm.pojo.Result;
import com.ssm.pojo.user.User;
import com.ssm.service.UserService;



@Service
public class UserServiceImpl implements UserService {
	@Autowired
	UserMapper userMapper;
	@Autowired
    MailSender mailSender;

	@Override
	public Result add(HttpSession session,User user) {
		// TODO Auto-generated method stub
		Result result = new Result();
		String userName = user.getUserName();
		String trueName = user.getTrueName();
		String password = user.getPassWord();
		if (userName == null || trueName == null) {
			result.setErrorCode(1);
			result.setErrorMsg("用户名或真实姓名不能为空");
			return result;
		}
		if (password.length() < 6) {
			result.setErrorCode(1);
			result.setErrorMsg("密码不能小于6位数");
			return result;
		}
		User userGet = userMapper.get(user);
		Integer resultAdd = 0;
		if(userGet == null) {
			user.setPassWord(DigestUtils.sha256Hex(user.getPassWord()));
			resultAdd = userMapper.add(user);
		}else {
			result.setErrorCode(1);
			result.setErrorMsg("用户名或邮箱已经使用");
			return result;
		}
		if (resultAdd.equals(1)) {
			result.setErrorCode(0);
			result.setErrorMsg("添加成功");
			session.removeAttribute("verifyCode");
		}else {
			result.setErrorCode(1);
			result.setErrorMsg("添加失败");
		}
		return result;
	}

	@Override
	@Transactional
	public int sendCode(User user,String explain) {
		// TODO Auto-generated method stub
		// 生成6位随机数字
        Integer code = (int)((Math.random()*9+1)*100000);
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setFrom("clig@foxmail.com");
		mailMessage.setSubject("验证码："+code);
		mailMessage.setText("您好 "+user.getUserName()+" ，"+explain+"，您的验证码为："+code+" ，此验证码有效期为10分钟，只能使用一次！");
		mailMessage.setTo(user.getEmail());
		mailSender.send(mailMessage);
		return code;
	}

	@Override
	public Result login(HttpSession session,User user) {
		// TODO Auto-generated method stub
		Result result = new Result();
		user.setEmail(user.getUserName());
		User userLogin = userMapper.get(user);
		User userTemp = userLogin;
		if (userTemp == null) {
			result.setErrorCode(1);
			result.setErrorMsg("用户未注册");
			return result;
		}
		String password = userMapper.getPassword(user);
		Boolean flag = DigestUtils.sha256Hex(user.getPassWord()).equals(password);
		if(!flag) {
			result.setErrorCode(1);
			result.setErrorMsg("密码错误");
			return result;
		}
		session.setMaxInactiveInterval(60*60*24*30);
		session.setAttribute("userId",userLogin.getUserId());
		session.setAttribute("userName",userLogin.getUserName());
		session.setAttribute("trueName",userLogin.getTrueName());
		session.setAttribute("email",userLogin.getEmail());
		result.setErrorCode(0);
		result.setErrorMsg("登录成功");
		return result;
	}

	@Override
	public Result updateUser(HttpSession session,User user) {
		// TODO Auto-generated method stub
		String userName = user.getUserName();
		String trueName = user.getTrueName();
		Result result = new Result();
		if (userName.isEmpty() || trueName.isEmpty()) {
			result.setErrorCode(1);
			result.setErrorMsg("用户名或真实姓名不能为空");
			return result;
		}
		Integer updateResult = userMapper.update(user);
		if (updateResult.equals(1)) {
			result.setErrorCode(0);
			result.setErrorMsg("修改成功");
			session.setAttribute("userName",userName);
			session.setAttribute("trueName",trueName);
			return result;
		}
		result.setErrorCode(1);
		result.setErrorMsg("修改失败");
		return result;
	}

	@Override
	public Result updateUserPassword(HttpSession session,User user) {
		// TODO Auto-generated method stub
		String password = user.getPassWord();
		Result result = new Result();
		if (password.length() < 6) {
			result.setErrorCode(1);
			result.setErrorMsg("密码不能小于6位数");
			return result;
		}
		user.setPassWord(DigestUtils.sha256Hex(user.getPassWord()));
		Integer updateResult = userMapper.updatePassword(user);
		if (updateResult.equals(1)) {
			result.setErrorCode(0);
			result.setErrorMsg("修改成功");
			session.removeAttribute("verifyCode");
			return result;
		}
		result.setErrorCode(1);
		result.setErrorMsg("修改失败");
		return result;
	}

	@Override
	public Result updateUserEmail(HttpSession session,User user) {
		// TODO Auto-generated method stub
		Result result = new Result();
		Integer emailResult = userMapper.updateEmail(user);
		if (emailResult.equals(1)) {
			result.setErrorCode(0);
			result.setErrorMsg("修改成功");
			// 移除验证码session
			session.removeAttribute("verifyCode");
			session.removeAttribute("autoVerifyCode");
			session.setAttribute("email", user.getEmail());
			return result;
		}
		result.setErrorCode(1);
		result.setErrorMsg("修改失败");
		return result;
	}

	@Override
	public User get(User user) {
		// TODO Auto-generated method stub
		return userMapper.get(user);
	}
}
